package richbar.com.richbar.activity.me;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ToggleButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.common.base.BaseActivity;
import com.common.base.BaseResult;
import com.common.help.RecycleViewHelp;
import com.common.util.LogUtil;
import com.common.util.ToastUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import richbar.com.richbar.R;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.bean.AddressDefaultResult;
import richbar.com.richbar.bean.AddressResult;
import richbar.com.richbar.help.TitleHelp;
import richbar.com.richbar.util.LoadingDialog;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;
import richbar.com.richbar.view.dialog.TipDialog;

/**
 * Created by QiYang on 2018/10/31.
 * 管理收货地址
 */

public class ManageGoodsAddressAc extends BaseActivity {
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.swipe)
    SwipeRefreshLayout swipe;
    private MyAdapter adapter;
    private int clickPos = -1;
    private LoadingDialog loadingDialog;
    List<AddressResult.AddressBean> data;

    @Override
    public int initLayout() {
        return R.layout.ac_manage_goods_address;
    }

    @Override
    public void initTitle() {
        new TitleHelp(this).setTitle("管理收货地址").setRightTv("添加", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startForResult(AddAddressAc.class, 201);
            }
        });
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        loadingDialog = new LoadingDialog(this, 0);
        adapter = new MyAdapter(R.layout.item_address, new ArrayList<AddressResult.AddressBean>());
        RecycleViewHelp.setManager(recycleView, R.drawable.line_8, this);
        recycleView.setAdapter(adapter);
        swipe.setRefreshing(true);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData();
            }
        });
    }

    @Override
    public void initData() {
        getData();
    }

    private void getData() {
        clickPos = -1;
        NetWorkUtil.addRessList(BaseApplication.uid, Util.md5(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                refreshDismiss(swipe);
                final AddressResult result = MyGson.getInstance().fromJson(data, AddressResult.class);
                if (checkIsOk(result)) {
                    setData(result);
                }
            }
        });
    }

    private void setData(AddressResult result) {
        data = result.getData().getList();
        adapter.setNewData(data);
        if (null == data || data.size() == 0) {
            View emptyView = LayoutInflater.from(ManageGoodsAddressAc.this).inflate(R.layout.view_empty_address, null);
            emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            emptyView.findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    start(AddAddressAc.class);
                }
            });
            adapter.setEmptyView(emptyView);
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        LogUtil.e("LLLLLLLL", "1");
        if (requestCode == 201 && resultCode == RESULT_OK) {
            LogUtil.e("LLLLLLLL", "2");
            getData();
        }
    }

    class MyAdapter extends BaseQuickAdapter<AddressResult.AddressBean> {
        //private List<Boolean> defaultAddress;
        private int defaultPos = -1;

        public MyAdapter(int layoutResId, List<AddressResult.AddressBean> data) {
            super(layoutResId, data);
            /*defaultAddress = new ArrayList<>();
            if (null != data && data.size() > 0)
            for (int i = 0; i < data.size(); i++) {
                defaultAddress.add(false);
            }*/
        }

        @Override
        protected void convert(final BaseViewHolder holder, final AddressResult.AddressBean bean) {
            LinearLayout view1 = holder.getView(R.id.editor_body);
            LinearLayout view2 = holder.getView(R.id.delete_body);
            if (clickPos == holder.getAdapterPosition() && holder.getAdapterPosition() != data.size() - 1) {
                view1.setVisibility(View.VISIBLE);
                view2.setVisibility(View.VISIBLE);
            } else {
                view1.setVisibility(View.GONE);
                view2.setVisibility(View.GONE);
            }
            holder.setText(R.id.name, bean.getName());
            holder.setText(R.id.tel, bean.getMobile());
            holder.setText(R.id.address, bean.getFulladdress());

            ToggleButton btn = holder.getView(R.id.default_address);


            if (defaultPos == -1) {
                LogUtil.e("JDJDJDJDJJD", "1");
                if (bean.getDef().equals("2")) {
                    btn.setChecked(true);
                } else {
                    btn.setChecked(false);
                }
            } else {
                LogUtil.e("JDJDJDJDJJD", "2");
                if (defaultPos == holder.getAdapterPosition()) {
                    btn.setChecked(true);
                } else {
                    btn.setChecked(false);
                }
            }


            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickPos = holder.getAdapterPosition();
                    notifyDataSetChanged();
                }
            });


            holder.setOnClickListener(R.id.editor_body, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    start(EditorAddressAc.class, new OnBundleListener() {
                        @Override
                        public Bundle onSetBundle(Bundle bundle) {
                            bundle.putSerializable("bean", (Serializable) bean);
                            return bundle;
                        }
                    });
                }
            });
            holder.setOnClickListener(R.id.delete_body, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showDelete(bean, holder.getAdapterPosition());
                }
            });

            holder.setOnClickListener(R.id.default_address, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //defaultAddress.set(holder.getAdapterPosition(),true);
                    defaultPos = holder.getAdapterPosition();
                    setDefault(bean.getId());
                }
            });
        }

        private void setDefault(String id) {
            loadingDialog.show();
            NetWorkUtil.setDefaultAddress(id, new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    final AddressDefaultResult result = MyGson.getInstance().fromJson(data, AddressDefaultResult.class);
                    if (checkIsOk(result)) {
                        if (result.getData().getResult() == 1) {
                            loadingDialog.dismiss();
                            notifyDataSetChanged();
                        }
                    }
                }
            });
        }

        private void showDelete(final AddressResult.AddressBean bean, int adapterPosition) {
            TipDialog.show(ManageGoodsAddressAc.this, "确定删除地址？", new TipDialog.OnListener() {
                @Override
                public void onBesure() {
                    NetWorkUtil.delAddRess(bean.getId(), Util.md5(), new HttpUtils.HttpCallback() {
                        @Override
                        public void onSuccess(String data) {
                            final BaseResult result = MyGson.getInstance().fromJson(data, BaseResult.class);
                            if (null != result && result.getCode().equals("200")) {
                                ManageGoodsAddressAc.this.getData();
                            } else {
                                ToastUtil.showToast(ManageGoodsAddressAc.this, "删除失败");
                            }
                        }
                    });
                }
            });

        }
    }

}
